const { app, BrowserWindow, dialog, ipcMain } = require('electron')
const path = require('node:path')
const os = require('os')
const main = require('./main')

let DesktopPath=os.homedir() + '\\Desktop';

async function handleFileOpen() {
    const { canceled, filePaths } = await dialog.showOpenDialog({
        defaultPath: DesktopPath,
        filters: [{ name: 'Images', extensions: ['jpg', 'png'] }],
        properties: ['openFile'],
    });
    if (!canceled) {
        DesktopPath=filePaths[0].split('\\'+filePaths[0].split('\\').pop())[0];

        return filePaths[0]
    }
}
async function handleFileOpens() {
    const { canceled, filePaths } = await dialog.showOpenDialog({
        defaultPath:DesktopPath,
        filters: [{ name: 'Images', extensions: ['jpg', 'png'] }],
        properties: ['openFile', 'multiSelections'],
    });
    if (!canceled) {

        DesktopPath=filePaths[0].split('\\'+filePaths[0].split('\\').pop())[0];

        return filePaths
    }
}

async function TaskStart(e,mainImg,imgs) {
    return await main.main(mainImg,imgs);
}



const createWindow = () => {
    const win = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            nodeIntegrationInWorker: true,
            preload: path.join(__dirname, 'preload.js')
        },
        autoHideMenuBar: true, // 隐藏顶部操作栏
    })

    win.loadFile('index.html');
}

app.whenReady().then(() => {
    ipcMain.handle('dialog:openFile', handleFileOpen)
    ipcMain.handle('dialog:openFiles', handleFileOpens)
    ipcMain.handle('dialog:TaskStart', TaskStart)
    createWindow()
    app.on('activate', function () {
        if (BrowserWindow.getAllWindows().length === 0) createWindow()
    });
})

